package com.xmqian.app.project.ui.main;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.coszero.ui.dialog.ToastUtils;
import com.coszero.utils.utils.LogX;
import com.coszero.utils.utils.RandomUtils;
import com.xmqian.app.project.common.base.BaseMyActivity;
import com.xmqian.app.project.event.LoginEvent;
import com.xmqian.app.project.ui.preview.activity.TestMainActivity;
import com.xmqian.kotlinApp.R;
import com.xmqian.kotlinApp.databinding.ActivityMainBinding;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class MainActivity extends BaseMyActivity {
    private static final long DELAY_TIME = 1500;//退出间隔时间
    private long lastPressBackKeyTime = 0;
    SwipeRefreshLayout swipeRefreshLayout;
    TextView tvGo;
    private Handler handler;
    private ActivityMainBinding binding;

    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    @SuppressWarnings({"unchecked", "deprecation"})
    public void initView() {
        swipeRefreshLayout = binding.swipeRefreshLayout;
        tvGo = binding.tvGo;
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                tvGo.setTextColor(RandomUtils.getRandownColor());
//                super.handleMessage(msg);
                handler.sendEmptyMessageDelayed(1, 1000);
            }
        };

        swipeRefreshLayout.setOnRefreshListener(() -> {
            handler.removeMessages(1);
            handler.sendEmptyMessage(1);
            swipeRefreshLayout.setRefreshing(false);
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - lastPressBackKeyTime < DELAY_TIME) {
            super.onBackPressed();
        } else {
            lastPressBackKeyTime = System.currentTimeMillis();
            ToastUtils.showLongMsg(getResources().getString(R.string.toast_press_again_exit_app));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventLogin(LoginEvent event) {
        if (event.isLogin()) {
            LogX.i("### 登录成功");
        } else {
            LogX.i("### 退出登录成功");
        }
    }

    void onClick() {
        startActivity(new Intent(this, TestMainActivity.class));
        finish();
    }

    @Override
    public void onDestroy() {
        if (handler != null) {
            handler.removeMessages(1);
            handler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();
    }

    @Override
    protected LinearLayout getLayoutView() {
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }
}
